<%--
  Created by IntelliJ IDEA.
  User: fengdefangxiang
  Date: 2022/1/18
  Time: 11:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="/web/header.jsp" %>
</head>
<body>
<fieldset class="layui-elem-field" style="margin: 20px;padding: 15px">
    <legend>设置部门</legend>
    <form class="layui-form" lay-field="formA" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-inline">
                <input type="text" name="code" readonly lay-verify="required" placeholder="请输入编号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <select name="codeDept"></select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="button" value="确定" class="layui-btn" lay-submit lay-filter="setDept">
                <input type="button" value="关闭" class="layui-btn" onclick="layerClose()">
            </div>
        </div>
        <input type="hidden" name="action" value="setDept">
    </form>
</fieldset>
<script type="text/javascript">
    initSel();

    function initSel() {   // 从数据库中初始化下拉框中的数据
        ajax('/DepartmentServlet', {action: 'list'}, 'json', function (data) {
            let opt = "<option value=''>请选择部门</option>"
            $.each(data, function () {
                opt += "<option value='" + this.code + "'>" + this.name + "</option>"
            })
            $("select[name='codeDept']").html(opt);
            form.render();  // 改变了表单，要对表单进行重新渲染
            init();         // 回显 放在这里不放在外面是因为 ：：异步请求
        })
    }

    var code = '<%=request.getParameter("code")%>';

    function init() {
        ajax('/EmployeeServlet', {code: code, action: 'get'}, 'json', function (data) {
            // 回显
            $("input[name='code']").val(data.code)
            //form.val("formA", {code: data.code, codeDept: data.codeDept})
        });
    }

    formOnSubmit('setDept', '/EmployeeServlet', 'text', function (data) {
        if (data == 1) {
            layer.msg("设置成功", layerClose)
        } else if (data == 0) {
            layer.msg("部门名称为空")
        } else {
            layer.msg("设置失败")
        }
    })
</script>
</body>
</html>
